package com.etc.controller;


import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.etc.entity.Jonseeker;
import com.etc.service.JonseeService;


@Controller
public class UploadController {
	
	String str = "D:\\tomcat\\tomcat-8\\webapps\\maven_jobSys\\myfiles\\";
	@Autowired
	JonseeService jonservice;
	
	@PostMapping(value = "filec")
	public String uploadfile(HttpServletRequest request,String resumeFile,
			@RequestParam(value = "myfile") MultipartFile mfile) {
		
		resumeFile =str+mfile.getOriginalFilename();
		
		Jonseeker jonse = new Jonseeker();
		jonse.setUserId(6);
		jonse.setResumeFile(resumeFile);
		  
		jonservice.updatajon(jonse);
		// 1 获取“文件” 获取参数
		System.out.println(str+mfile.getOriginalFilename());
		// mfile
		System.out.println(mfile.getContentType() + "," + mfile.getName() + "," + mfile.getOriginalFilename() + ","
				+ mfile.getSize());



		String pathname = request.getRealPath("/");
		System.out.println("pathname :" + pathname);

		// destFile是 完整的目录+文件名
		File destFile = new File(pathname + "myfiles/" + mfile.getOriginalFilename());

		// 2 将file [MultipartFile] 对象存储起来

		// ① 使用原始文件流来操作 myfile.get
		// ② 使用封装好的方法来写
		// 第一个参数 要写入目标位置的文件流
		// FileUtils.copyInputStreamToFile(mfile.getInputStream(), destFile);
		// File对象 java.io.File

		// ③MultipartFile的方法
		try {
			mfile.transferTo(destFile);
		} catch (IllegalStateException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "redirect:jonsee/6";

	}

}
